<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录网上书城</view>
			<input class="u-border-bottom" v-model="email" placeholder="请输入邮箱帐号" />
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">忘记密码</view>
				<navigator class="issue" url="./register">注册帐号</navigator>
			</view>
		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">网上书城用户协议、隐私政策。</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				password: '' 
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (uni.$u.test.email(this.email) && this.password) {
					style.color = "#fff";
					style.backgroundColor = uni.$u.color['warning'];
				}
				return style;
			}
		},
		onLoad() {
			
		},
		methods: {
			async submit() {
				if (uni.$u.test.email(this.email) && this.password) {
					try{
						let res = await uni.$u.http.post('/api/auth/login',
							 {email:this.email,password:this.password}
						);
						//如果登录失败，会返回false
						if(!res){
							return;
						}
						console.log(res);						
						uni.setStorage({
							key:'access_token',
							data:res.access_token
						})
						//登录成功后，获取个人信息。
						res = await uni.$u.http.get('/api/user',{header:{Authorization:'bearer ' + res.access_token}});
						uni.setStorageSync('userinfo',res);
						uni.$u.toast('登录成功');
						//登录成功后跳转至登录前所在的页面
						const backUrl =  uni.getStorageSync('backUrl') || 'pages/index/index';
						console.log(backUrl);
						setTimeout(()=>{
							uni.$u.route({
								type:'reLaunch',  
								url:backUrl
							})
						},1500)					
					}
					catch(e){
						console.log(e);
						uni.setStorage({
							key:'access_token',
							data:''
						})
						uni.$u.toast('登录失败');
					}
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.getCaptcha{margin: 40rpx 0;}
	.u-border-bottom,.hint {
		margin-bottom: 40rpx !important;
	}

	.wrap {
		font-size: 28rpx;
	
	.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 350rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
